﻿using System.Collections.Generic;
using System.Runtime.Serialization;
using GMME.Util;
using UnityEngine;
using UnityEngine.Scripting;

namespace GMME.Model.Rtm.Result
{
    [DataContract]
    public class RtmChannelMemberInfo
    {
        /**
         * 开发者Id
         */
        [DataMember(Name = "openId")]
        [Preserve]
        public string OpenId { get; set; }

        /**
         * RTM连接状态: 未连接(0),已连接(1)
         */
        [DataMember(Name = "status")]
        [Preserve]
        public int Status { get; set; }

        /**
         * 玩家属性
         */
        [DataMember(Name = "playerProperties")]
        [Preserve]
        public Dictionary<string, string> PlayerProperties { get; set; }

        [DataMember(Name = "playerPropertiesInner")]
        [Preserve]
        public List<Property> PlayerPropertiesInner { get; set; }


#if UNITY_ANDROID
        internal static RtmChannelMemberInfo ConvertRtmChannelMemberInfo(
            AndroidJavaObject javaObject)
        {
            if (javaObject != null)
            {
                RtmChannelMemberInfo rtmChannelMemberInfo = new();
                rtmChannelMemberInfo.OpenId = javaObject.Call<string>("getOpenId");
                rtmChannelMemberInfo.Status = javaObject.Call<int>("getStatus");
                var androidJavaObject = javaObject.Call<AndroidJavaObject>("getPlayerProperties");
                rtmChannelMemberInfo.PlayerProperties = TransferUtil.MapToDict(androidJavaObject);
                return rtmChannelMemberInfo;
            }

            return null;
        }

#endif
        public void ConvertPropertiesToDict()
        {
            var dict = new Dictionary<string, string>();
            if (PlayerPropertiesInner != null)
                foreach (var property in PlayerPropertiesInner)
                    dict.Add(property.Key, property.Value);

            PlayerProperties = dict;
        }
    }
}